package com.xteam.web.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.tangkf.utils.CommonUtils;
import com.tangkf.utils.bean.BeanHelper;
import com.tangkf.utils.bean.ReqHelper;
import com.xteam.db.page.Page;
import com.xteam.db.page.PageInfo;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.purview.entities.PurviewRole;
import com.xteam.purview.entities.PurviewRoleFunDatarule;
import com.xteam.purview.entities.PurviewUser;
import com.xteam.purview.service.impl.AuthorizationFace;
import com.xteam.purview.service.impl.PurviewRoleFace;
import com.xteam.utils.WebTools;
import com.xteam.web.Error;

public class RoleController extends PurViewController implements IController {

	private PurviewRoleFace	face	= PurviewRoleFace.getInstance();
	private AuthorizationFace	aface	= AuthorizationFace.getInstance();
	
	@Autowired
	private SystemLogService logService;

	public RoleController() {
	}
	
	public ModelAndView setPurview(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.save(request, response);
		
		String roleId = ReqHelper.getRequestString(request, PurviewRole.PROP_ROLEID);
		String[] funId = ReqHelper.getRequestStrings(request, "funId[]");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String typeId = "1";
		
		int r = aface.addRoleFunNodeRelation(roleId, funId, typeId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_USER_MGR, 
				"addRoleFunNodeRelation", ConstantLocation.LOG_SET_ROLEPUR, "PurviewRoleFunDatarule");
		
		System.err.println(r);
		return null;
	}
	
	/**
	 * 返回角色关联的功能节点
	 * @author tangkf  ---2012-2-8
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView getRoleFunNodeRelation(HttpServletRequest request, HttpServletResponse response) throws Exception {
		setJsonType(request,response);
		
		String roleId = ReqHelper.getRequestString(request, PurviewRole.PROP_ROLEID);
		List<PurviewRoleFunDatarule> list = aface.getRoleFunNodeRelation(roleId);
		
		PrintWriter out = response.getWriter();
		String data = JSONArray.fromObject(list).toString();
		out.println(data);
		
		System.err.println(list);
		return null;
	}
	
	public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.save(request, response);
		
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		String lslMethod = "save";
		
		PurviewRole fn = new PurviewRole();
		BeanHelper.requestToBean(fn, request);
		int r	= 0;
		
		if (CommonUtils.isEmpty(fn.getRoleId())) {
			r	= face.save(fn);
		} else {
			r	= face.update(fn);
			lslType = ConstantLocation.LOG_MODIFY;
			lslMethod = "update";
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ROLE_MGR, 
				lslMethod, lslType, "PurviewRole");
		
		Error er	= r>0?Error.create(r+"", "保存角色出错！"):Error.create();
		WebTools.writerJsonToResponse(response, er);
		return null;
	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.delete(request, response);
		
		String[] ids = ReqHelper.getRequestStrings(request, "roleId[]");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		int r	= face.delBatch(PurviewRole.class, PurviewRole.PROP_ROLEID, ids);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ROLE_MGR, 
				"delBatch", ConstantLocation.LOG_DELETE, "PurviewRole");
		
		Error er	= r>0?Error.create(r+"", "删除角色出错！"):Error.create();
		WebTools.writerJsonToResponse(response, er);
		return null;
	}

	public ModelAndView find(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.find(request, response);
		
		int size = ReqHelper.getRequestInt(request, "size");
		int pageNo = ReqHelper.getRequestInt(request, "pageNo");

		Page page = new PageInfo(size, pageNo + 1);

		PrintWriter out = response.getWriter();
		String data = JSONObject.fromObject(face.query(PurviewRole.class, page)).toString();
		
		System.err.println(data);
		
		out.println(data);
		return null;
	}
	
	public ModelAndView getUserListByRoleId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.setJsonType(request, response);
		String roleId = ReqHelper.getRequestString(request, "roleId");
		PrintWriter out = response.getWriter();
		List<PurviewUser> list = face.getUserListByRoleId(roleId);
		String rinf = "[]";
		if (list != null && list.size() > 0)
			rinf = JSONArray.fromObject(list).toString();
		out.println(rinf);
		return null;
	}

	/**
	 * 保存用户角色关系
	 * @author 2012-9-26
	 * @throws Exception
	 */
	public ModelAndView saveUserRole(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.setJsonType(request, response);
		
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		int r = 0;
		String roleId = ReqHelper.getRequestString(request, "roleId");
		String[] userId = ReqHelper.getRequestStrings(request, "userId[]");
		r = face.saveUserRole(roleId, userId);

		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ROLE_MGR, 
				"saveUserRole", ConstantLocation.LOG_BUND_USER, "PurviewUserRole");
		
		Error er = r > 0 ? Error.create(r + "", "设置用户出错！") : Error.create();
		WebTools.writerJsonToResponse(response, er);
		return null;
	}
	
	public ModelAndView delRoleUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.setJsonType(request, response);
		int r = 0;
		
		String roleId = ReqHelper.getRequestString(request, "roleId");
		String[] userId = ReqHelper.getRequestStrings(request, "userId[]");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		r = face.delRoleUser(roleId, userId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ROLE_MGR, 
				"delRoleUser", ConstantLocation.LOG_DELETE, "PurviewUserRole");

		Error er = r > 0 ? Error.create(r + "", "删除角色用户出错！") : Error.create();
		WebTools.writerJsonToResponse(response, er);
		return null;
	}
	
}
